﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Model;

namespace NHibernate.DAL
{
    /// <summary>
    /// 分页功能
    /// </summary>
    public class PagingBase
    {
        /// <summary>
        /// Linq数据分页
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="query">数据集</param>
        /// <param name="p">分页条件</param>
        /// <returns></returns>
        public static IQueryable<T> Paging<T>(IQueryable<T> query, BaseQuery p)
        {
            p.RecordCount = query.Count();
            if ((p.Index - 1) * p.PageSize > p.RecordCount)
            {
                int maxIndex = p.RecordCount / p.PageSize;
                if (p.RecordCount % p.PageSize > 0)
                {
                    maxIndex++;
                }
                p.Index = maxIndex;
            }
            query = query.Skip((p.Index - 1) * p.PageSize).Take(p.PageSize);
            return query;
        }

        /// <summary>
        /// Linq数据分页
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="query">数据集</param>
        /// <param name="p">分页条件</param>
        /// <returns></returns>
        public static IEnumerable<T> Paging<T>(IEnumerable<T> query, BaseQuery p)
        {
            p.RecordCount = query.Count();
            if ((p.Index - 1) * p.PageSize > p.RecordCount)
            {
                int maxIndex = p.RecordCount / p.PageSize;
                if (p.RecordCount % p.PageSize > 0)
                {
                    maxIndex++;
                }
                p.Index = maxIndex;
            }
            query = query.Skip((p.Index - 1) * p.PageSize).Take(p.PageSize);
            return query;
        }

        /// <summary>
        /// SQL数据分页
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="tableName">查询对象名</param>
        /// <param name="whereStr">查询条件(and 字段名=字段值)</param>
        /// <param name="p">分页条件</param>
        /// <returns></returns>
        public static List<T> Paging<T>(string tableName, string whereStr, BaseQuery p)
        {
            if (string.IsNullOrEmpty(p.SortName))
            {
                p.SortName = "CURRENT_TIMESTAMP";
                p.SortFlag = "";
            }
            List<T> list = new List<T>();
            string countStr = " select count(*) from " + tableName + " where 1=1 ";
            StringBuilder sql = new StringBuilder();
            sql.Append(" SELECT TOP (" + p.PageSize + ")* FROM (");
            sql.Append(" select *,ROW_NUMBER() OVER(ORDER BY " + p.SortName + " " + p.SortFlag + ") as sort_row from " + tableName + " where 1=1 ");
            sql.Append(whereStr);
            sql.Append(")as query WHERE query.sort_row >" + (p.PageSize * (p.Index - 1)) + " ORDER BY query.sort_row ");

            System.Data.DataSet ds = LYM.CommonWeb.DataServer.SqlHelper.ExecuteGetDataSet(DBConfig.Temporary, System.Data.CommandType.Text, countStr + whereStr + sql.ToString());
            if (ds.Tables.Count > 0)
            {
                p.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                if (ds.Tables[1].Rows != null)
                    list = LYM.CommonWeb.DataServer.SqlHelper.DataConvert<T>(ds.Tables[1]);
            }
            return list;
        }
    }
}
